<template>
  <div>
    <div class="tw-flex tw-justify-center tw-mt-4">
      <el-input prefix-icon="el-icon-search" v-model="keywords" v-on:keyup.native.enter="doSearch"
                class="tw-w-3/12" clearable @clear="doSearch"></el-input>
      <el-button class="tw-ml-1" type="primary" @click="doSearch">搜索</el-button>
    </div>
    <div class="tw-grid tw-grid-cols-3">

      <el-skeleton style="width: 400px;margin-top: 10px" v-for="i in 3" :key="'a'+i" :loading="loading" animated>
        <template slot="template">
          <el-card style="width: 400px">
            <div slot="header" style="width: 200px">
              <el-skeleton-item variant="h1" style="width: 150px"/>
            </div>
            <div class="card-info">
              <el-skeleton-item variant="image" style="height: 100px; width: 100px;
            border-radius: 50px;margin: 0 auto"/>
              <div>
                <el-skeleton-item variant="h1" style="width: 200px"/>
              </div>
              <div>
                <el-skeleton-item variant="h1" style="width: 300px"/>
              </div>
              <div>
                <el-skeleton-item variant="h1" style="width: 100px"/>
              </div>
              <div>
                <el-skeleton-item variant="h1" style="width: 200px"/>
              </div>
            </div>
          </el-card>
        </template>
      </el-skeleton>

      <el-card class="tw-m-4" v-for="item in admins" :key="item.id">
        <div slot="header" class="clearfix">
          <span>{{ item.name }}</span>
          <el-button style="float: right; padding: 3px 0;color: red" icon="el-icon-delete"
                     @click="deleteAdmin(item)"></el-button>
        </div>
        <div class="tw-text-blue-600">
          <div class="admin-info">
            <img class="tw-w-3/12 tw-float-left tw-mr-2" :src="item.userFace"/>
            <div class="tw-pt-4">
              <div>手机号码：{{ item.phone }}</div>
              <div>电话号码：{{ item.telephone }}</div>
            </div>
          </div>
          <div class="admin-info tw-clear-left tw-pt-2">
            <div>地址：{{ item.address }}</div>
            <div>用户状态：
              <el-switch
                  style="margin-left: 8px"
                  v-model="item.enabled"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  @change="changeAdmin(item)"
                  inactive-text="禁用">
              </el-switch>
            </div>
            <div>
              用户角色：
              <el-tag type="success" class="tw-mx-1" v-for="role in item.roles" :key="role.id">
                {{ role.nameZh }}
              </el-tag>
              <el-popover
                  placement="top-start"
                  title="角色列表"
                  width="200"
                  @show="selectRolesChange(item)"
                  @hide="updateAdminRoles(item)"
                  trigger="click">
                <el-select v-model="selectRoles" multiple placeholder="请选择">
                  <el-option
                      v-for="(role,i) in allRoles"
                      :key="i"
                      :label="role.nameZh"
                      :value="role.id">
                  </el-option>
                </el-select>
                <el-button slot="reference" icon="el-icon-more" type="text">修改</el-button>
              </el-popover>
            </div>
            <div>备注：{{ item.remark }}</div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>

export default {
  name: "SysAdmin",
  methods: {
    updateAdminRoles(admin) {
      if (admin.roles.map(item => item.id).toString() === this.selectRoles.toString()) {
        return
      }
      let url = this.api.admin + `role?adminId=${admin.id}`
      this.selectRoles.forEach(item => {
        url += `&rids=${item}`
      })
      this.putRequest(url).then(res => {
        if (res) {
          this.initAdmin()
        }
      })
    },
    selectRolesChange(admin) {
      this.selectRoles = []
      this.selectRoles = admin.roles.map(item => item.id)
    },
    initAllRoles() {
      this.getRequest(this.api.admin + 'roles').then(res => {
        this.allRoles = res.data
        console.log(this.allRoles)
      })
    },
    changeAdmin(admin) {
      this.putRequest(this.api.admin, admin).then(res => {
        if (res) {
          this.initAdmin()
        }
      })
    },
    deleteAdmin(admin) {
      this.commonUtil.oneConfirmDialog(admin.name, () => {
        this.deleteRequest(this.api.admin + admin.id).then(res => {
          if (res) {
            this.initAdmin()
          }
        })
      }, "操作员")
    },
    doSearch() {
      this.initAdmin()
    },
    initAdmin() {
      this.getRequest(this.api.admin + `?keywords=${this.keywords}`).then(res => {
        if (res) {
          this.admins = res.data
          this.loading=false
        }
      })
    }
  },
  data() {
    return {
      allRoles: [],
      admins: [],
      keywords: '',
      selectRoles: [],
      loading:true
    }
  },
  mounted() {
    this.initAdmin()
    this.initAllRoles()
  }
}
</script>

<style scoped>

.admin-info > div {
  margin-top: 5px;
}

.card-info > div {
  margin-top: 5px;
}

</style>